<template>
  <div class="wrapper">
    <img
      class="wrapper__img"
      src="http://www.dell-lee.com/imgs/vue3/user.png"
      alt=""
    />
    <div class="wrapper__ipt">
      <input
        type="text"
        class="wrapper__ipt__content"
        v-model="data.username"
        placeholder="请输入用户名"
      />
    </div>
    <div class="wrapper__ipt">
      <input
        type="text"
        class="wrapper__ipt__content"
        v-model="data.password"
        placeholder="请输入密码"
      />
    </div>
    <div class="wrapper__ipt-button" @click="handleLogin">登录</div>
    <div class="wrapper__ipt-link" @click="handleRegisterClick">立即注册</div>
    <Toast v-if="toastData.showToast" :msg="toastData.toastMsg" />
  </div>
</template>
<script>
import { useRouter } from "vue-router";
import { reactive } from "vue";
import { post } from "../../utils/request";
import Toast, { useToastEffect } from "../../components/Toast";
import { useLocalDataEffect } from "../../localData/localData";
const localData = [
  {
    userName: "吴淑亚",
    password: "111111",
  },
  {
    userName: "Lisa",
    password: "222222",
  },
  {
    userName: "Jane",
    password: "333333",
  },
];
const setLocalData = (localData) => {
  const localDataString = JSON.stringify(localData);
  sessionStorage.localData = localDataString;
};
setLocalData(localData);
const useRegisterEffcet = () => {
  const router = useRouter();
  const handleRegisterClick = () => {
    router.push({ name: "Register" });
  };
  return { handleRegisterClick };
};
const useLoginEffect = (showToast) => {
  const router = useRouter();
  const data = reactive({ username: "", password: "" });
  const handleLogin = async () => {
    try {
      const result = await post("/api/user/login", {
        username: data.username,
        password: data.password,
      });
      const { comNameAndPwd } = useLocalDataEffect(
        data.username,
        data.password
      );
      const flag = comNameAndPwd(data.username, data.password);
      if (result.data.errno === 0) {
        if (flag === true) {
          localStorage.isLogin = true;
          router.push({ name: "Home" });
        } else {
          if (data.username === "") {
            showToast("请输入用户名和密码");
          } else {
            showToast("密码输入错误");
          }
        }
      } else {
        showToast("最后的路径有问题登录失败");
      }
    } catch (e) {
      showToast("baseUrl有问题请求失败");
    }
  };
  return { data, handleLogin };
};
export default {
  name: "Login",
  components: { Toast },
  setup() {
    const { toastData, showToast } = useToastEffect();
    const { data, handleLogin } = useLoginEffect(showToast);
    const { handleRegisterClick } = useRegisterEffcet();
    return {
      handleLogin,
      handleRegisterClick,
      data,
      toastData,
      showToast,
    };
  },
};
</script>
<style lang="scss" scoped>
.wrapper {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  transform: translateY(-50%);
  .wrapper__img {
    display: block;
    width: 0.66rem;
    height: 0.66rem;
    margin: 0 auto 0.4rem auto;
  }
  .wrapper__ipt {
    height: 0.48rem;
    margin: 0 0.4rem 0.2rem 0.4rem;
    .wrapper__ipt__content {
      box-sizing: border-box;
      padding: 0 0.2rem 0 0.2rem;
      width: 100%;
      height: 100%;
      font-size: 0.16rem;
      background-color: #f9f9f9;
      border: 0.01rem solid rgba(0, 0, 0, 0.1);
      border-radius: 0.06rem;
      outline: none;
      &::placeholder {
        color: #777;
      }
    }
  }
  .wrapper__ipt-button {
    height: 0.48rem;
    margin: 0.32rem 0.4rem 0.2rem 0.4rem;
    text-align: center;
    line-height: 0.48rem;
    font-size: 0.16rem;
    color: #fff;
    background-color: #0091ff;
    box-shadow: 0 0.04rem 0.08rem 0 rgba(0, 145, 255, 0.32);
    border-radius: 0.06rem;
  }
  .wrapper__ipt-link {
    text-align: center;
    font-size: 0.14rem;
    line-height: 0.2rem;
    color: #777;
  }
}
</style>
